﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParsingProject
{
    public static class FileFactory
    {
        public static bool WriteAsCSV(string path, List<List<string>> lines)
        {
            if (!ParserHelper.CreateFile(path))
            {
                return false;
            }

            try
            {
                foreach (var line in lines)
                {
                    foreach (var cell in line)
                    {
                        ParserHelper.Write(path, cell + ",");
                    }
                    ParserHelper.WriteLine(path);
                }
            }
            catch (IOException e)
            {
                Console.Error.WriteLine(e.Message);
                return false;
            }
            finally
            {
                ParserHelper.CloseWriteFile(path);
            }

            return true;
        }

        private static bool writeAsJSON(string path, List<string> types, List<List<string>> lines,
            bool ignoreLastColumn)
        {
            if (!ParserHelper.CreateFile(path))
            {
                return false;
            }

            var nline = lines.Count;
            var ncol = types.Count;

            if (ignoreLastColumn)
            {
                ncol--;
            }

            try
            {
                ParserHelper.WriteLine(path, "[");

                for (int i = 0; i < nline; ++i)
                {
                    ParserHelper.WriteLine(path, "\t{");

                    for (int j = 0; j < ncol; ++j)
                    {
                        ParserHelper.Write(path, "\t\t\"");
                        ParserHelper.Write(path, types[j]);
                        ParserHelper.Write(path, "\" : \"");
                        ParserHelper.Write(path, lines[i][j]);
                        ParserHelper.Write(path, "\"");

                        if (j != ncol - 1)
                        {
                            ParserHelper.Write(path, ",");
                        }

                        ParserHelper.WriteLine(path);
                    }

                    ParserHelper.Write(path, "\t}");

                    if (i != nline - 1)
                    {
                        ParserHelper.Write(path, ",");
                    }

                    ParserHelper.WriteLine(path);
                }

                ParserHelper.WriteLine(path, "]");
            }
            finally
            {
                ParserHelper.CloseWriteFile(path);
            }

            return true;
        }
            

        public static bool WriteAsJSON(string path, List<List<string>> lines, int records,
            bool ignoreLastColumn = false)
        {
            var types = lines[0];
            var splitter = 0;

            while (splitter < lines.Count)
            {
                var upperBound = splitter + records;

                if (upperBound >= lines.Count)
                {
                    upperBound = lines.Count - 1;
                }

                var csplit = splitter / records;
                var npath = path.Insert(0, csplit.ToString());

                if (!writeAsJSON(npath, types, lines.GetRange(splitter, upperBound - splitter),
                    ignoreLastColumn))
                {
                    return false;
                }

                splitter = upperBound;
            }
           

            return true;
        }
    }
}
